// pages/doctor-authentication-2/doctor-authentication-2.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    imageone: "",
    imagetwo: "",
    imagethree: "",
    cardFront:"",
    cardOpposite:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    this.setData({
      name: e.name,
      avatarUrls: e.avatarUrls,
      hospital: e.hospital,
      provinceid: e.provinceid,
      cityid: e.cityid,
      districtid: e.districtid,
      lng: e.lng,
      lat: e.lat,
      address: e.address,
      content: e.content,
      service: e.service, 
      position: e.position,     
      cardNo: e.cardNo,
      dage: e.dage,
      hospitalLevel: e.hospitalLevel,
      birth: e.birth,
      hospitalTypeName: e.hospitalTypeName                                                              
    })
    console.log(e.birth)
    wx.setStorageSync("userInformation", e);
    this.dataSecondShow();
  },
  // 数据二次回显
  dataSecondShow() {

    if (wx.getStorageSync("IDcardFace")) {
      const IDcardFace = wx.getStorageSync("IDcardFace");
      this.setData({
        cardFront: IDcardFace.cardFront
      })
    }
    if (wx.getStorageSync("IDcardBack")) {
      const IDcardBack = wx.getStorageSync("IDcardBack");
      this.setData({
        cardOpposite: IDcardBack.cardOpposite
      })
    }

    if (wx.getStorageSync("certificateOne")) {
      const certificateOne = wx.getStorageSync("certificateOne");
      this.setData({
        imageone: certificateOne.imageone
      })
    }
    if (wx.getStorageSync("certificateTwo")) {
      const certificateTwo = wx.getStorageSync("certificateTwo");
      this.setData({
        imagetwo: certificateTwo.imagetwo
      })
    }
    if (wx.getStorageSync("certificateThree")) {
      const certificateThree = wx.getStorageSync("certificateThree");
      this.setData({
        imagethree: certificateThree.imagethree
      })
    }
  },
  //保存
  addsave: function() {
    const { name, avatarUrls, hospital, provinceid, cityid, districtid, content, imageone, imagetwo, imagethree, lng, lat, address, service, cardFront, cardOpposite, position, cardNo, dage, birth, hospitalLevel} = this.data;
                      
    if (cardFront == '') {
      wx.showToast({
        title: '请上传身份证正面照片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (cardOpposite == '') {
      wx.showToast({
        title: '请上传身份证背面照片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (imageone == '') {
      wx.showToast({
        title: '请上传医生职业证书复印件',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (imagetwo == '') {
      wx.showToast({
        title: '请上传医师资格证',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (imagethree == '') {
      wx.showToast({
        title: '请上传近期体检表',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
  
    app.request({
      url: 'doctor/doctor/finish',
      data: {
        photo: avatarUrls,
        name,
        company: hospital,
        service,
        proId: provinceid,
        areaId: cityid,
        townId: districtid,
        intro: content,
        seniority: imageone,
        profession: imagetwo,
        beauty: imagethree,
        lat,
        lng,
        cardFront,
        cardNo,
        employmentYear: dage.substr(0, dage.length - 1),
        birth: birth.substr(0, dage.length - 1),
        hospitalLevel,
        cardOpposite,      
        address,
        position
      },
      success(res) {
        wx.navigateTo({
          url: '../doctor-authentication-3/doctor-authentication-3',
        })
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        });
      }
    })
  },
  // 身份证正面上传图片
  cardFront: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original"],
      sourceType: ["album", "camera"],
      success: function (e) {
        
        var a = e.tempFilePaths[0];
        app.uploadFile({
          url: 'doctor/doctor/uploadCertFile',
          filePath: a,
          success: function (res) {
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              cardFront: path
            })
            wx.setStorageSync("IDcardFace", {
              cardFront: path
            })
          },
          fail: function (e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 身份证反面上传图片
  cardOpposite: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original"],
      sourceType: ["album", "camera"],
      success: function (e) {
     
        var a = e.tempFilePaths[0];
        app.uploadFile({
          url: 'doctor/doctor/uploadCertFile',
          filePath: a,
          success: function (res) {
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              cardOpposite: path
            })
            wx.setStorageSync("IDcardBack", {
              cardOpposite: path
            })
          },
          fail: function (e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 职业证
  updateone: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original"],
      sourceType: ["album", "camera"],
      success: function(e) {
      
        var a = e.tempFilePaths[0];
        app.uploadFile({
          url: 'doctor/doctor/uploadCertFile',
          filePath: a,
          success: function(res) {
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              imageone: path
            })
            wx.setStorageSync("certificateOne", {
              imageone: path
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 资格证
  updatetwo: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original"],
      sourceType: ["album", "camera"],
      success: function(e) {
      
        var a = e.tempFilePaths[0];
        app.uploadFile({
          url: 'doctor/doctor/uploadCertFile',
          filePath: a,
          success: function(res) {
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              imagetwo: path
            })
            wx.setStorageSync("certificateTwo", {
              imagetwo: path
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  },
  // 体检表
  updatethree: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original"],
      sourceType: ["album", "camera"],
      success: function(e) {
       
        var a = e.tempFilePaths[0];
        app.uploadFile({
          url: 'doctor/doctor/uploadCertFile',
          filePath: a,
          success: function(res) {
            var path = JSON.parse(res.data).data[0].filePath;
            that.setData({
              imagethree: path
            })
            wx.setStorageSync("certificateThree", {
              imagethree: path
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })
  }
})